package uit.nfcmenu.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Base64;
import android.util.Log;

public final class ConvertImg {
	static String tag = ConvertImg.class.getSimpleName();
	
	// convert from bitmap to byte array
	public static byte[] getBytes(Bitmap bitmapPNG) {
		if (bitmapPNG == null) {
			return null;
		}
		
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		bitmapPNG.compress(CompressFormat.PNG, 0, outputStream);
		
		byte[] result = outputStream.toByteArray();
		try {
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		outputStream = null;
	//	bitmap.recycle();// TODO research 1
		return result;
	}
	
	// convert from drawable to byte array
	public static byte[] getBytes(Context context, int drawableImg) {
		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
				drawableImg);
		
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.PNG, 0, outputStream);
		byte[] result = outputStream.toByteArray();
		try {
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		outputStream = null;
		//bitmap.recycle();// TODO research 1
		return result;
	}
	
	// convert from byte array to bitmap
	public static Bitmap getBitmap(byte[] bytes) {
		
		if (bytes != null) {
			return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
		}
	    return null;
	}
	
	// convert from byte array to bitmap
	public static Drawable getDrawable(Context mContext, Bitmap bitmap) {
		if (bitmap == null) {
			return null;
		}
		return (new BitmapDrawable(mContext.getResources(),bitmap));
	}
	
	public static Drawable getDrawable(Bitmap bitmap) {
		if (bitmap == null) {
			return null;
		}
		return (new BitmapDrawable(bitmap));
	}
	
	public static Drawable getDrawable(byte[] bytes) {
		if (bytes != null) {
			Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
			BitmapDrawable result = new BitmapDrawable(bitmap);
			//bitmap.recycle();// TODO research 1
			return result;
		}
		return null;
	}
	
	public static Drawable getDrawable(Context context, byte[] bytes) {
		if (bytes != null) {
			Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
			BitmapDrawable result = new BitmapDrawable(context.getResources(), bitmap);
			//bitmap.recycle();// TODO research 1
			return result;
		}
		return null;
	}
	
	public static String encodeBase64(Bitmap bitmap) {
		String imageEncoded = null;
		if (bitmap != null) {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
			byte[] bytes = baos.toByteArray();
			imageEncoded = Base64.encodeToString(bytes, Base64.DEFAULT);
		}
		
		Log.i(tag, "encodeBase64(): "+(imageEncoded==null?"null"
				:imageEncoded.subSequence(0, 10)+"..."+imageEncoded.length()));
		return imageEncoded;
	}

	public static Bitmap decodeBase64(String input) {
		Bitmap bitmap = null;
		if (input != null) {
			byte[] decodedByte = Base64.decode(input, 0);
			bitmap = BitmapFactory
					.decodeByteArray(decodedByte, 0, decodedByte.length);
		}
		
		Log.i(tag, "decodeBase64() return "+(bitmap==null?"null"
				:bitmap.getWidth()+"x"+bitmap.getHeight()));
		return bitmap;
	}
}
